import requests
import time
from urllib.parse import urlencode
import json

# 配置
base_url = "http://www.nmc.cn/rest/weather"
station_id = [59493,59312,50953]  # 你要查询的气象站ID 深圳：59493、潮州：59312、中山：59485、哈尔滨：50953

#生成13位时间戳
timestamp = int(time.time() * 1000)
result_str = "【天气预告】\n"
result_str += f'-----------------------\n'
for i in station_id:
    # 构建查询参数
    query_parameters = {
        'stationid': i,
        '_': timestamp
    }

    # 将参数编码并拼接到URL中
    url = f"{base_url}?{urlencode(query_parameters)}"

    # print(url)

    # 发送GET请求获取网页内容
    response = requests.get(url)

    # 检查响应状态码
    if response.status_code == 200:
        # 打印网页内容
        # print(response.text)

        # json文本处理
        python_obj = json.loads(response.text)
        python_data = python_obj["data"]
            # tempchart   #15天气温情况
            # predict     #7天的气温详情
        #输出相关内容
        result_str += f'{python_data["real"]["station"]["province"]}:{python_data["real"]["station"]["city"]}\n'
        result_str += f'当前气温:{python_data["real"]["weather"]["temperature"]} {python_data["real"]["weather"]["info"]}\n'

        result_str += f'今天气温:{python_data["predict"]["detail"][0]["day"]["weather"]["temperature"]}至{python_data["predict"]["detail"][0]["night"]["weather"]["temperature"]}, 度{python_data["predict"]["detail"][0]["day"]["weather"]["info"]}\n'
        result_str += f'明天气温:{python_data["predict"]["detail"][1]["day"]["weather"]["temperature"]}至{python_data["predict"]["detail"][0]["night"]["weather"]["temperature"]}度,{python_data["predict"]["detail"][1]["day"]["weather"]["info"]}\n'
        result_str += f'后天气温:{python_data["predict"]["detail"][2]["day"]["weather"]["temperature"]}至{python_data["predict"]["detail"][2]["night"]["weather"]["temperature"]}度，{python_data["predict"]["detail"][2]["day"]["weather"]["info"]}\n'
        # print(python_data["real"]["station"]["province"],python_data["real"]["station"]["city"])
        # print("当前气温:", python_data["real"]["weather"]["temperature"], python_data["real"]["weather"]["info"])
        # print("今天气温:", python_data["predict"]["detail"][0]["day"]["weather"]["temperature"], "至",
        #       python_data["predict"]["detail"][0]["night"]["weather"]["temperature"], "度",
        #       python_data["predict"]["detail"][0]["day"]["weather"]["info"])
        # print("明天气温:", python_data["predict"]["detail"][1]["day"]["weather"]["temperature"], "至",
        #       python_data["predict"]["detail"][1]["night"]["weather"]["temperature"],"度",
        #       python_data["predict"]["detail"][1]["day"]["weather"]["info"])
        # print("后天气温:", python_data["predict"]["detail"][2]["day"]["weather"]["temperature"], "至",
        #       python_data["predict"]["detail"][2]["night"]["weather"]["temperature"],"度",
        #       python_data["predict"]["detail"][2]["day"]["weather"]["info"])
        # print("-----------------------")
        result_str += f'-----------------------\n'
    else:
        print(f"Failed to retrieve the content. Status code: {response.status_code}")

print(result_str)

#发送vx消息
url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=d230f478-24eb-4780-a45d-0e8fe951bded'
headers = {'Content-Type': 'application/json'}
#文本
#带超链接
#article ="<a href=\"http://baidu.com\">邮件中心视频实况</a>"


data = {
    "msgtype": "text",
    "text": {
        "content": result_str
    },
    "safe": 0,
    "enable_id_trans": 0,
    "enable_duplicate_check": 0,
    "duplicate_check_interval": 1800
}
response = requests.post(url, headers=headers, data=json.dumps(data))